package com.bbc.base;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import com.bbc.R;
import com.bbc.uitls.L;

/**
 * Created by Administrator on 2016/12/8 0008.
 */

public abstract class ContentPager extends FrameLayout {
    public ContentPager(Context context) {
        this(context, null);
    }

    public ContentPager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    //自定义模块界面：errorView,successView,emptyView,loadingView;
    private View mErrorView, mSuccessView, mEmptyView, mLoadingView;
    //初始状态状态
    private final static int STATE_DEFAULT = 0;
    public final static int STATE_SUCCESS = 1;
    public final static int STATE_EMPTY = 2;
    private final static int STATE_LOADING = 3;
    public final static int STATE_ERROR = 4;
    //默认状态
    private int state = STATE_DEFAULT;

    public ContentPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化页面
        initPager((Activity) context);
    }

    //初始化页面
    private void initPager(Activity mActivity) {
        //添加出错的界面，访问网络出错的界面
        if (mErrorView == null) {
            mErrorView = onCreateErrorView();
            addView(mErrorView);
        }
        if (mEmptyView == null) {
            mEmptyView = onCreateEmptyView();
            addView(mEmptyView);
        }
        if (mLoadingView == null) {
            mLoadingView = onCreateLoadingView();
            addView(mLoadingView);
        }
        //成功界面比较复杂，最好不要初始化，等到网络真的访问成功了再去访问
        //初始化显示一次
        showPager();
    }

    //显示界面的方法
    private void showPager() {
        //显示加载界面， 当是加载状态和缺省状态都显示转圈圈的效果
        if (mLoadingView != null) {
            if (state == STATE_LOADING || state == STATE_DEFAULT) {
                mLoadingView.setVisibility(View.VISIBLE);
            } else {
                mLoadingView.setVisibility(View.GONE);
            }
        }
        //显示网络网络出错界面
        if (mErrorView != null) {
            mErrorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.GONE);
        }
        //显示空界面
        if (mEmptyView != null) {
            mEmptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.GONE);
        }
        //这里才要去加载主界面,当网络访问成功了。并且mSuccessView为空要去初始化
        if (mSuccessView == null) {
            //但是这里不知道成功后到底显示什么界面，所以需要提供一个抽象方法给外界使用
            mSuccessView = onCreateSuccessView();
            if (mSuccessView != null) {
                this.addView(mSuccessView);
                L.d("wgc", "成功访问");
            } else {
                throw new IllegalArgumentException("创建成功界面失败，参数异常");
            }
        }
        //成功的View初始化之后，进行显示
        if (mSuccessView != null) {
            mSuccessView.setVisibility(state == STATE_SUCCESS ? View.VISIBLE : View.GONE);
        }
    }

    //访问网络获取数据,或者刷新数据
    public void loadDataOrRefreshData(ResultState resultState) {
        //1.除了正在加载数据以外，我都要去访问网络
        //2.重置状态
        if (state == STATE_SUCCESS || state == STATE_EMPTY || state == STATE_ERROR) {
            state = STATE_DEFAULT;
        }
        //当重置后的状态是STATE_DEFAULT就开始访问网络 ，排除正在加载状态 防止重复访问网络
        if (state == STATE_DEFAULT) {
            //开始访问网络
            //ThreadPoolManager.getThreadPoolProxy().execute(new LoadDataRunnable(resultState));
            state = resultState.getState();
            showPager();
        }
    }

    //创建错误界面
    private View onCreateErrorView() {
        return Global.inflate(R.layout.layout_error);
    }

    private View onCreateLoadingView() {
        return Global.inflate(R.layout.layout_loading);
    }

    private View onCreateEmptyView() {
        return Global.inflate(R.layout.layout_empty);
    }

    //创建成功界面，因为成功界面控件不知道要显示什么，所以要抽象出去给使用者实现
    protected abstract View onCreateSuccessView();
}
